import { _decorator, Camera, Component, Node, tween, Vec3 } from 'cc';
import { PrepareUI } from '../UI/PrepareUI';
import { SunManager } from './SunManager';
import { ZombieManager } from './ZombieManager';
import { CardListUI } from '../UI/CardListUI';
import { FailUI } from '../UI/FailUI';
import { WinUI } from '../UI/WinUI';
import { AudioManager } from './AudioManager';
import { Config } from './Config';
const { ccclass, property } = _decorator;

@ccclass('GameManager')
export class GameManager extends Component {
    private static _instance: GameManager = null;
    @property(Node)
    camera: Node = null;
    @property(PrepareUI)
    prepareUI:PrepareUI;
    @property(CardListUI)
    cardListUI:CardListUI;
    @property(FailUI)
    failUI:FailUI;
    @property(WinUI)
    WinUI:WinUI;
    isGameEnd:boolean = false;

    /**
     * 获取实例
     */
    public static get Instance(): GameManager {
        return this._instance;
    }
    protected onLoad(): void {
        //确保全局只有一个实例
        if (GameManager._instance == null) {
            GameManager._instance = this;
        } else {
            console.log("GameManager is already exist");
            this.node.destroy();
        }
        this.GameStart();
    }
    protected onDestroy(): void {
        if(GameManager.Instance==this){
            GameManager._instance=null;
            this.node.destroy();
        }
    }

    GameStart() {
        tween(this.camera)
            // 将当前节点位置动画移动到中间节点位置
            .to(3, { position: new Vec3(375, 0, 0) }, { easing: 'cubicOut' })
            .to(3, { position: new Vec3(0, 0, 0) }, {
                easing: 'cubicOut',
                onComplete: (target?: object) => {
                    // 回调，当缓动动作更新时触发。
                    this.onCameraMoveComplete();
                },
            })
            // 启动动画
            .start();
    }
    public gameOver(){
        if(this.isGameEnd) return;
        this.isGameEnd = true;
        this.failUI.show();
        ZombieManager.Instance.Pause();
        SunManager.Instance.pauseProduceSun();
        this.cardListUI.disableCardList();
        AudioManager.Instance.playClip(Config.lose_music);
    }

    public gameVictory(){
        if(this.isGameEnd) return;
        this.isGameEnd = true;
        this.WinUI.show();
        this.cardListUI.disableCardList();
        AudioManager.Instance.playClip(Config.win_music);

    }
    onCameraMoveComplete(){
        this.prepareUI.show(this.onPrepreUIComplete);
    }
    onPrepreUIComplete(){
        SunManager.Instance.startProduceSun();
        ZombieManager.Instance.startSpawn();
        this.cardListUI.showCardList();
        AudioManager.Instance.playBGM(Config.bgm1);
    }
}


